package com.coffeeloversupportlibrary.coffeelover.domain.article.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.cleveroad.fanlayoutmanager.FanLayoutManager;
import com.cleveroad.fanlayoutmanager.FanLayoutManagerSettings;
import com.coffeeloversupportlibrary.R;
import com.coffeeloversupportlibrary.coffeelover.domain.article.entity.Article;
import com.coffeeloversupportlibrary.coffeelover.domain.article.type.RecommendationType;
import com.coffeeloversupportlibrary.databinding.RecommendationShowBinding;

import java.util.List;

/**
 * @author puyv
 * @createTime 2021/11/6 20:16
 * @projectName CoffeeLoverSupportLibrary
 * @className RecommendationAdapter.java
 * @description TODO
 */
public class RecommendationAdapter implements Adapter<List<RecommendationType>> {
    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent) {
        View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.recommendation_show,parent,false);
        RecommendationShowBinding recommendationShowBinding=RecommendationShowBinding.bind(view);
        return new RecommendationArticleViewHolder(recommendationShowBinding);
    }

    @Override
    public void onBindViewHolder(@NonNull List<RecommendationType> items, @NonNull RecyclerView.ViewHolder holder, int position) {
        RecommendationType item=items.get(position);
        List<Article> list=item.getItems();

        FanLayoutManagerSettings fanLayoutManagerSettings = FanLayoutManagerSettings
                .newBuilder( ((RecommendationArticleViewHolder)holder)
                        .recommendationShowBinding.getRoot().getContext())
                .withFanRadius(true)
                .withAngleItemBounce(1)
                .withViewWidthDp(100)
                .withViewHeightDp(130)
                .build();

        FanLayoutManager fanLayoutManager=new FanLayoutManager(((RecommendationArticleViewHolder)holder).recommendationShowBinding.getRoot().getContext(),fanLayoutManagerSettings);
        RecommendationListAdapter recommendationListAdapter=new RecommendationListAdapter(list,fanLayoutManager,((RecommendationArticleViewHolder)holder).recommendationShowBinding.RecommendationItemView);
        ((RecommendationArticleViewHolder)holder).recommendationShowBinding.RecommendationItemView.setAdapter(recommendationListAdapter);
        fanLayoutManager.switchItem(((RecommendationArticleViewHolder)holder).recommendationShowBinding.RecommendationItemView,3);
        ((RecommendationArticleViewHolder)holder).recommendationShowBinding.RecommendationItemView.setLayoutManager(fanLayoutManager);
        fanLayoutManager.scrollToPosition(3);

    }

    static class RecommendationArticleViewHolder extends RecyclerView.ViewHolder{
        private RecommendationShowBinding recommendationShowBinding;
        public RecommendationArticleViewHolder(@NonNull RecommendationShowBinding recommendationShowBinding) {
            super(recommendationShowBinding.getRoot());
            this.recommendationShowBinding=recommendationShowBinding;
        }
    }
}


//    private MediaPlayer mediaPlayer = new MediaPlayer();
//
//    @Override
//    protected void onCreate(Bundle savedInstanceState) {
//        super.onCreate(savedInstanceState);
//        setContentView(R.layout.activity_main);
//
//        Button btnPlay = (Button) findViewById(R.id.btnPlay);
//        Button btnPause = (Button) findViewById(R.id.btnPause);
//        Button btnStop = (Button) findViewById(R.id.btnStop);
//
//        btnPlay.setOnClickListener(this);
//        btnPause.setOnClickListener(this);
//        btnStop.setOnClickListener(this);
//
//
//        //权限判断，如果没有权限就请求权限
//        if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
//            ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
//        } else {
//            initMediaPlayer();//初始化播放器 MediaPlayer
//        }
//
//
//    }
//
//    private void initMediaPlayer() {
//        try {
////            File file = new File(Environment.getExternalStorageDirectory(), "music.mp3");
//            AssetManager am = getAssets();
//            AssetFileDescriptor afd = am.openFd("WyvernP -  Palescreen.mp3");
//            FileDescriptor fd = afd.getFileDescriptor();
//
//            mediaPlayer.setDataSource(fd,afd.getStartOffset(),afd.getLength());//指定音频文件路径
//            mediaPlayer.setLooping(true);//设置为循环播放
//            mediaPlayer.prepare();//初始化播放器MediaPlayer
//
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//    }
//
//    @Override
//    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
//        switch (requestCode){
//            case 1:
//                if(grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
//                    initMediaPlayer();
//                }else{
//                    Toast.makeText(this, "拒绝权限，将无法使用程序。", Toast.LENGTH_LONG).show();
//                    finish();
//                }
//                break;
//            default:
//        }
//
//    }
//
//    @Override
//    public void onClick(View v) {
//        switch (v.getId()){
//            case R.id.btnPlay:
//
//                //如果没在播放中，立刻开始播放。
//                if(!mediaPlayer.isPlaying()){
//                    mediaPlayer.start();
//                }
//                break;
//            case R.id.btnPause:
//                //如果在播放中，立刻暂停。
//                if(mediaPlayer.isPlaying()){
//                    mediaPlayer.pause();
//                }
//                break;
//            case R.id.btnStop:
//                //如果在播放中，立刻停止。
//                if(mediaPlayer.isPlaying()){
//                    mediaPlayer.reset();
//                    initMediaPlayer();//初始化播放器 MediaPlayer
//                }
//                break;
//            default:
//                break;
//        }
//    }
//
//    @Override
//    protected void onDestroy() {
//        super.onDestroy();
//        if(mediaPlayer != null){
//            mediaPlayer.stop();
//            mediaPlayer.release();
//        }
//    }
//
